package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.entity.ProductCategory;
import com.southwind.entity.ProductInfo;
import com.southwind.form.SellerProductAddForm;
import com.southwind.form.SellerProductUpdateForm;
import com.southwind.result.ResponseEnum;
import com.southwind.service.ProductCategoryService;
import com.southwind.service.ProductInfoService;
import com.southwind.util.ResultVOUtil;
import com.southwind.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author southwind
 * @since 2023-12-27
 */
@RestController
@RequestMapping("/seller/product")
public class SellerProductController {


    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private ProductInfoService productInfoService;

    @GetMapping("findAllProductCategory")
    public ResultVO findAllProductCategory() {
        List<ProductCategory> productCategoryList = productCategoryService.list();
        List<SellerProductCategoryVO> sellerProductCategoryVOList = new ArrayList<>();
        for (ProductCategory productCategory : productCategoryList) {
            SellerProductCategoryVO sellerProductCategoryVO = new SellerProductCategoryVO();
            sellerProductCategoryVO.setType(productCategory.getCategoryType());
            sellerProductCategoryVO.setName(productCategory.getCategoryName());
        }
        Map<String, List<SellerProductCategoryVO>> map = new HashMap<>();
        map.put("content", sellerProductCategoryVOList);
        return ResultVOUtil.success(map);
    }

    /**
     * 添加商品的接口
     *
     * @param form
     * @return
     */
    @PostMapping("add")
    public ResultVO add(@RequestBody SellerProductAddForm form) {
        ProductInfo productInfo = new ProductInfo();
        BeanUtils.copyProperties(form, productInfo);
        boolean save = this.productInfoService.save(productInfo);
        if (save) return ResultVOUtil.success(null);
        return ResultVOUtil.fail(ResponseEnum.ADD_PRODUCT_ERROR.getMsg());

    }


    /**
     * 分页查询 商品信息
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("list/{page}/{size}")
    public ResultVO list(@PathVariable("page") Integer page,
                         @PathVariable("size") Integer size) {
//        // 单表分页查询
//        Page<ProductInfo> productInfoPage = new Page<>(page, size);
//        Page<ProductInfo> resPage = productInfoService.page(productInfoPage);
//        ContentVO contentVO = new ContentVO();
//        List<ProductInfo> list = resPage.getRecords();
//        List<SellerProductInfoVO> sellerProductInfoVOList = new ArrayList<>();
//        for (ProductInfo productInfo : list) {
//            SellerProductInfoVO sellerProductInfoVO = new SellerProductInfoVO();
//
//            sellerProductInfoVO.setId(productInfo.getProductId());
////            sellerProductInfoVO.setStatus(productInfo.getProductStatus());
//
//
//            sellerProductInfoVOList.add(sellerProductInfoVO);
//        }
//        contentVO.setContent(sellerProductInfoVOList);
//        contentVO.setTotal(resPage.getTotal());
//        contentVO.setSize((int) resPage.getSize());
//        Map<Object, Object> map = new HashMap<>();
//        map.put("content", sellerProductInfoVOList);
//        return ResultVOUtil.success(map);

        Page<ProductInfo> page1 = new Page<>(page, size);
        Page<ProductInfo> page2 = this.productInfoService.page(page1);
        List<SellerProductInfoVO> content = new ArrayList<>();
        for (ProductInfo record : page2.getRecords()) {
            SellerProductInfoVO vo = new SellerProductInfoVO();
            QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_type", record.getCategoryType());
            ProductCategory productCategory = this.productCategoryService.getOne(queryWrapper);
            vo.setCategoryName(productCategory.getCategoryName());
            vo.setPrice(record.getProductPrice());
            vo.setId(record.getProductId());
            vo.setDescription(record.getProductDescription());
            vo.setIcon(record.getProductIcon());
            vo.setName(record.getProductName());
            vo.setStock(record.getProductStock());
            if (record.getProductStatus() == 0) {
                vo.setStatus(false);
            } else {
                vo.setStatus(true);
            }
            content.add(vo);
        }
        ContentVO contentVO = new ContentVO();
        contentVO.setContent(content);
        contentVO.setSize(size);
        contentVO.setTotal(page2.getTotal());
        return ResultVOUtil.success(contentVO);
    }


    @GetMapping("/like/{keyWord}/{page}/{size}")
    public ResultVO like(
            @PathVariable("keyWord") String keyWord,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ) {
        Page<ProductInfo> page1 = new Page<>(page, size);
        QueryWrapper<ProductInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.like("product_name", keyWord);
        Page<ProductInfo> page2 = this.productInfoService.page(page1, queryWrapper1);
        List<SellerProductInfoVO> content = new ArrayList<>();
        for (ProductInfo record : page2.getRecords()) {
            SellerProductInfoVO vo = new SellerProductInfoVO();
            QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_type", record.getCategoryType());
            ProductCategory productCategory = this.productCategoryService.getOne(queryWrapper);
            vo.setCategoryName(productCategory.getCategoryName());
            vo.setPrice(record.getProductPrice());
            vo.setId(record.getProductId());
            vo.setDescription(record.getProductDescription());
            vo.setIcon(record.getProductIcon());
            vo.setName(record.getProductName());
            vo.setStock(record.getProductStock());
            if (record.getProductStatus() == 0) {
                vo.setStatus(false);
            } else {
                vo.setStatus(true);
            }
            content.add(vo);
        }
        ContentVO contentVO = new ContentVO();
        contentVO.setContent(content);
        contentVO.setSize(size);
        contentVO.setTotal(page2.getTotal());
        return ResultVOUtil.success(contentVO);
    }


    @GetMapping("/findByCategory/{categoryType}/{page}/{size}")
    public ResultVO findByCategory(
            @PathVariable("categoryType") Integer categoryType,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ) {
        Page<ProductInfo> page1 = new Page<>(page, size);
        QueryWrapper<ProductInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("category_type", categoryType);
        Page<ProductInfo> page2 = this.productInfoService.page(page1, queryWrapper1);
        List<SellerProductInfoVO> content = new ArrayList<>();
        for (ProductInfo record : page2.getRecords()) {
            SellerProductInfoVO vo = new SellerProductInfoVO();
            QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_type", record.getCategoryType());
            ProductCategory productCategory = this.productCategoryService.getOne(queryWrapper);
            vo.setCategoryName(productCategory.getCategoryName());
            vo.setPrice(record.getProductPrice());
            vo.setId(record.getProductId());
            vo.setDescription(record.getProductDescription());
            vo.setIcon(record.getProductIcon());
            vo.setName(record.getProductName());
            vo.setStock(record.getProductStock());
            if (record.getProductStatus() == 0) {
                vo.setStatus(false);
            } else {
                vo.setStatus(true);
            }
            content.add(vo);
        }
        ContentVO contentVO = new ContentVO();
        contentVO.setContent(content);
        contentVO.setSize(size);
        contentVO.setTotal(page2.getTotal());
        return ResultVOUtil.success(contentVO);
    }


    @GetMapping("/findById/{id}")
    public ResultVO findById(@PathVariable("id") Integer id) {
        // 通过id查询，实体类
        ProductInfo productInfo = this.productInfoService.getById(id);
        SellerProductInfoVO2 vo = new SellerProductInfoVO2();
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_type", productInfo.getCategoryType());
        ProductCategory productCategory = this.productCategoryService.getOne(queryWrapper);
        vo.setPrice(productInfo.getProductPrice());
        vo.setId(productInfo.getProductId());
        vo.setDescription(productInfo.getProductDescription());
        vo.setIcon(productInfo.getProductIcon());
        vo.setName(productInfo.getProductName());
        vo.setStock(productInfo.getProductStock());
        Map map = new HashMap();
        map.put("categoryType", productInfo.getCategoryType());
        vo.setCategory(map);
        return ResultVOUtil.success(vo);
    }

    @DeleteMapping("/delete/{id}")
    public ResultVO delete(@PathVariable("id") Integer id) {
        boolean remove = this.productInfoService.removeById(id);
        if (remove) return ResultVOUtil.success(null);
        return ResultVOUtil.fail(ResponseEnum.DELETE_PRODUCT_ERROR.getMsg());
    }

    @PutMapping("/updateStatus/{id}/{status}")
    public ResultVO updateStatus(@PathVariable("id") Integer id, @PathVariable("status") boolean status) {
        ProductInfo byId = this.productInfoService.getById(id);
        if (status) {
            byId.setProductStatus(1);
        } else {
            byId.setProductStatus(0);
        }
        boolean updateById = this.productInfoService.updateById(byId);
        if (updateById) return ResultVOUtil.success(null);
        return ResultVOUtil.fail(ResponseEnum.UPDATE_PRODUCT_STATUS_ERROR.getMsg());
    }


    @PutMapping("/update")
    public ResultVO update(@RequestBody SellerProductUpdateForm form){
        ProductInfo productInfo = this.productInfoService.getById(form.getId());
        if(form.getStatus()) {
            productInfo.setProductStatus(1);
        } else {
            productInfo.setProductStatus(0);
        }
        productInfo.setProductName(form.getName());
        productInfo.setProductPrice(form.getPrice());
        productInfo.setProductStock(form.getStock());
        productInfo.setProductDescription(form.getDescription());
        productInfo.setProductIcon(form.getIcon());
        productInfo.setCategoryType(form.getCategory().getCategoryType());
        boolean updateById = this.productInfoService.updateById(productInfo);
        if(updateById) return ResultVOUtil.success(null);
        return ResultVOUtil.fail(ResponseEnum.UPDATE_PRODUCT_ERROR.getMsg());
    }









}

